package dao;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import modelo.Cereal;
import modelo.CerealCosechaFina;
import modelo.Mineral;
import modelo.vista.CerealCosechaFinaView;

public class AdmPersistenciaCerealCosechaFina {

	private static AdmPersistenciaCerealCosechaFina instancia;

	public static AdmPersistenciaCerealCosechaFina getinstancia (){
		if(instancia==null)
			instancia = new AdmPersistenciaCerealCosechaFina();
		return instancia;

	}

	public Vector<Cereal> selectAll(){
		try{
			Vector<Cereal> resultados = new Vector<Cereal>();
			Connection connection = PoolConnection.getPoolConnecion().getConnection();
			Statement s = connection.createStatement();
			ResultSet rs = s.executeQuery("select * from Cereales");
			while(rs.next()){
				if(rs.getFloat(6)==0){
					int codigo = rs.getInt(1);
					String nombre = rs.getString(2);
					String epoca = rs.getString(3);
					float rinde =  rs.getFloat(4);
					Mineral mineral = Mineral.valueOf(rs.getString(5));
					CerealCosechaFina c = new CerealCosechaFina(codigo,new CerealCosechaFinaView(nombre,epoca,rinde,mineral));
					resultados.add(c);
					System.out.println("Se cargo cosecha fina: "+nombre);
				}
				PoolConnection.getPoolConnecion().releaseConnection(connection);
				return resultados;
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		return null;

	}

	public CerealCosechaFina selectCereal(int codigo){
		try{
			CerealCosechaFina cer = null;
			Connection connection = PoolConnection.getPoolConnecion().getConnection();
			PreparedStatement s= connection.prepareStatement("select * from Cereales where codigo =?");
			s.setInt(1, codigo);
			ResultSet rs = s.executeQuery();

			String nom =  rs.getString(2);
			String epsiem = rs.getString(3);
			float rin = rs.getFloat(4);
			Mineral ml = Mineral.valueOf(rs.getString(5));

			cer = new  CerealCosechaFina(nom,epsiem,rin,ml);
			PoolConnection.getPoolConnecion().releaseConnection(connection);
			return cer;
		}

		catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

	}

	public CerealCosechaFina selectCereal(String nombre){
		try{
			CerealCosechaFina cer = null;
			Connection connection = PoolConnection.getPoolConnecion().getConnection();
			PreparedStatement s= connection.prepareStatement("select * from Cereales where nombre =?");
			s.setString(1, nombre);
			ResultSet rs = s.executeQuery();

			if(rs.getRow()>0){
				String epsiem = rs.getString(3);
				float rin = rs.getFloat(4);
				Mineral ml = Mineral.valueOf(rs.getString(5));

				cer = new  CerealCosechaFina(nombre,epsiem,rin,ml);
				PoolConnection.getPoolConnecion().releaseConnection(connection);
				return cer;
			}
			else
				return null;
		}

		catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

	}

	public boolean existeCereal(String nombre) throws Exception{
		try{
			CerealCosechaFina cer = null;
			Connection connection = PoolConnection.getPoolConnecion().getConnection();
			PreparedStatement s= connection.prepareStatement("select * from Cereales where nombre = ?");
			s.setString(1, nombre);
			ResultSet rs = s.executeQuery();
			if(rs.getRow()==0)
				return false;
			else
				return true;
		}

		catch (SQLException e) {
			e.printStackTrace();
			throw new Exception("No se pudo verificar");

		}


	}

	public void insert (int c,String nombre, String epocasiembra, float rinde, Mineral mineral){
		try{
			Connection connection = PoolConnection.getPoolConnecion().getConnection();
			PreparedStatement s= connection.prepareStatement("insert into cereales (codigo,nombre,epoca_siembra,rinde,mineral, porcentaje_desperdicio) values (?,?,?,?,?,0)");
			s.setInt(1, c);
			s.setString(2, nombre);
			s.setString(3, epocasiembra);
			s.setFloat(4, rinde);
			s.setString(5, mineral.name());
			s.execute();
			PoolConnection.getPoolConnecion().releaseConnection(connection);
			System.out.println("Se ha incertado un cereal: "+c);
		}
		catch (SQLException e) {
			
			e.printStackTrace();
		}	
	}

	public void delete (int codigo){

		try{
			Connection connection = PoolConnection.getPoolConnecion().getConnection();

			PreparedStatement s= connection.prepareStatement("delete * from cereales where codigo = ?");
			s.setInt(1, codigo);
			s.executeQuery();
			PoolConnection.getPoolConnecion().releaseConnection(connection);
		}
		catch (SQLException e) {
			e.printStackTrace();
		}	
	}

	public void delete (String nombre){

		try{
			Connection connection = PoolConnection.getPoolConnecion().getConnection();

			PreparedStatement s= connection.prepareStatement("delete from cereales where nombre = ?");
			s.setString(1, nombre);
			s.execute();
			PoolConnection.getPoolConnecion().releaseConnection(connection);
		}
		catch (SQLException e) {
			e.printStackTrace();
		}	
	}

	public void update (CerealCosechaFina cereal){
		try{
			Connection connection = PoolConnection.getPoolConnecion().getConnection();


			PreparedStatement s= connection.prepareStatement("update cereales" +
					"set nombre = ?, epoca_siembra = ? , rinde = ?, mineral = ? where codigo = ? ");
			s.setString(1, cereal.getNombre());
			s.setString(2, cereal.getEpocaSiembra());
			s.setFloat(3, cereal.getRinde());
			s.setString(4, cereal.getMineralPrincipal().name()); 
			s.setLong(5, cereal.getCodigo());//pendiente
			s.executeQuery();
			PoolConnection.getPoolConnecion().releaseConnection(connection);

		}
		catch (SQLException e) {
			e.printStackTrace();
		}	


	}


}
